package com.blockmeta.bbs.businesslibrary.net.retrofit;


import com.blockmeta.bbs.baselibrary.net.Host;
import com.blockmeta.bbs.businesslibrary.base.eventbus.EventBusBase;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import retrofit2.Retrofit;


public class RestManager extends EventBusBase {


    private int NextTaskID = 0;

    public Map<String, Integer> mTaskIDs = new ConcurrentHashMap<String, Integer>();

    public int updateApiTaskID(String api, boolean replace) {
        synchronized (this) {
            ++NextTaskID;
            if (NextTaskID >= Short.MAX_VALUE) {
                NextTaskID = 1;
            }

            if (!replace) {
                Integer integer = mTaskIDs.get(api);
                if (integer != null) {
                    return -integer.intValue();
                }
            }
        }

        mTaskIDs.put(api, NextTaskID);

        return NextTaskID;
    }

    public boolean containsApiTaskID(String api, int taskID) {
        boolean ret = false;

        Integer integer = mTaskIDs.get(api);
        if (integer != null) {
            ret = integer.intValue() == taskID;
        }

        return ret;
    }

    public void clearApiTaskID(String api) {
        if (api == null) {
            return;
        }

        mTaskIDs.remove(api);
    }

    public static Retrofit getRetrofit() {
        return getRetrofit(null);
    }

    public static Retrofit getRetrofit(final String market) {
        String url = Host.BASE_URL;
        return new Retrofit.Builder()
                       .baseUrl(url)
                       .addConverterFactory(BTCConverterFactory.create())
                       .client(OkHttpUtils.getOkHttpClient())
                       .build();
    }
    protected String checkQuery(String query) {
        if (query != null && query.length() == 0) {
            return null;
        }
        return query;
    }


}
